
package com.jf.cloud.supplier.controller.platform;

import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.supplier.constant.AuditStatus;
import com.jf.cloud.supplier.dto.SupplierCompanyDTO;
import com.jf.cloud.supplier.model.SupplierCompany;
import com.jf.cloud.supplier.service.SupplierCompanyService;
import com.jf.cloud.supplier.vo.SupplierCompanyVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 供应商工商信息
 *
 * @author zz
 */
@RestController("platformSupplierCompanyController")
@RequestMapping("/p/supplier_company")
@Tag(name = "platform-供应商工商信息")
public class SupplierCompanyController {

    @Autowired
    private SupplierCompanyService supplierCompanyService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping
    @Operation(summary = "根据供应商id获取供应商工商信息" , description = "根据供应商id获取供应商工商信息")
    @Parameter(name = "status", description = "审核状态：1：已通过 0待审核 -1未通过" )
    public ServerResponseEntity<SupplierCompanyVO> getSupplierCompanyBySupplierId(@RequestParam("supplierId") Long supplierId, @RequestParam(value = "status",required = false, defaultValue = "1") Integer status) {
        return ServerResponseEntity.success(supplierCompanyService.getSupplierCompanyBySupplierIdAndStatus(supplierId, status));
    }

    @PutMapping
    @Operation(summary = "更新供应商工商信息" , description = "更新供应商工商信息")
    public ServerResponseEntity<Void> editSupplierCompany(@RequestBody @Valid SupplierCompanyDTO supplierCompanyDTO) {
        if (Objects.isNull(supplierCompanyDTO.getSupplierId())) {
            throw new LuckException("供应商id不能为空");
        }
        SupplierCompany supplierCompany = mapperFacade.map(supplierCompanyDTO, SupplierCompany.class);
        supplierCompany.setStatus(AuditStatus.SUCCESSAUDIT.value());
        supplierCompanyService.updateBySupplierId(supplierCompany, supplierCompany.getSupplierId(), 1);
        return ServerResponseEntity.success();
    }
}
